package world.hezi.callback;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import world.hezi.bot.BeatsaverBot;
import world.hezi.entity.PicInfo;
import world.hezi.mapper.PicInfoMapper;

import java.io.FileOutputStream;
import java.io.IOException;

public class TencentPicturesHttpCallback implements Callback {
    private final String uuid;
    private final PicInfoMapper picInfoMapper;
    private final PicInfo picInfo;

    public TencentPicturesHttpCallback(String uuid, PicInfoMapper picInfoMapper, PicInfo picInfo) {
        this.uuid = uuid;
        this.picInfoMapper = picInfoMapper;
        this.picInfo = picInfo;
    }

    @Override
    public void onFailure(@NotNull Call call, @NotNull IOException e) {

    }
    @Override
    public void onResponse(@NotNull Call call, @NotNull Response response) {
        ResponseBody body = response.body();
        FileOutputStream fileOutputStream;
        try {
            fileOutputStream = new FileOutputStream(BeatsaverBot.PIC_PATH+uuid);
            fileOutputStream.write(body.bytes());
            fileOutputStream.close();
            picInfoMapper.insert(picInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
